<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    <style type="text/css">
        .democlass{
            color:red;
        }
    </style>
</head>
<body>

<h1>Hello World</h1>
<p id="demo">单击按钮设置H1的class属性为“democlass”</p>
<button onclick="myFunction()">点我</button>
<script>
    function myFunction(){
        let h=document.getElementsByTagName("H1")[0];
        let typ=document.createAttribute("class");
        typ.nodeValue="democlass";
        h.attributes.setNamedItem(typ);
    }
</script>

</body>
</html>